package com.example.javaui.component;

import com.example.javaui.ResourceTable;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.render.*;
import ohos.agp.utils.Color;
import ohos.agp.utils.Point;
import ohos.agp.utils.RectFloat;
import ohos.agp.utils.TextAlignment;
import ohos.app.Context;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;
import ohos.media.image.common.PixelFormat;
import ohos.media.image.common.Rect;
import ohos.media.image.common.Size;

import java.io.InputStream;

public class NeumorTextButton extends Component implements Component.DrawTask {


    // HiLogLabel
    private static final HiLogLabel sLogLabel = new HiLogLabel(HiLog.LOG_APP, 0x00101, "NeumorButton");


    public NeumorTextButton(Context context) {
        super(context);
        initView();
    }

    public NeumorTextButton(Context context, AttrSet attrSet) {
        super(context, attrSet);
        initView();
    }

    public NeumorTextButton(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        initView();
    }

    public NeumorTextButton(Context context, AttrSet attrSet, int resId) {
        super(context, attrSet, resId);
        initView();
    }

    private void initView() {
        this.addDrawTask(this);
    }

    @Override
    public void onDraw(Component component, Canvas canvas) {

        int width = getWidth();
        int height = getHeight();
        int shadowWidth = 40;
        int centerX = width / 2;
        int centerY = height / 2;
        int realWidth = width - shadowWidth * 2;
        int realHeight = height - shadowWidth * 2;

        RectFloat rect = new RectFloat(shadowWidth, shadowWidth, realWidth + shadowWidth, realHeight + shadowWidth);
        canvas.drawRect(rect, newCirclePaint(centerX, centerY, shadowWidth));

        int textSize = 50;
        Paint textPaint2 = new Paint();
        textPaint2.setTextSize(textSize);
        textPaint2.setColor(Color.LTGRAY);
        textPaint2.setTextAlign(TextAlignment.HORIZONTAL_CENTER);
        canvas.drawText(textPaint2, "关注", centerX + 3, centerY + textSize / 2 + 3);

        Paint textPaint = new Paint();
        textPaint.setTextSize(textSize);
        textPaint.setTextAlign(TextAlignment.HORIZONTAL_CENTER);
        canvas.drawText(textPaint, "关注", centerX, centerY + textSize / 2);

    }

    private static Paint newCirclePaint(int centerX, int centerY, int shadowWidth) {

        Paint paint = new Paint();
        paint.setCornerPathEffectRadius(40);
        Point point1 = new Point(centerX,centerY);
        Point point2 = new Point(centerX + shadowWidth,centerY + shadowWidth * centerX / centerY );
        Point[] newPoints = new Point[]{point1, point2};
        Color[] newColors = new Color[]{Color.WHITE, Color.GRAY};
        Shader.TileMode tileMode = Shader.TileMode.CLAMP_TILEMODE;
        LinearShader shader = new LinearShader(newPoints, null, newColors, tileMode);
        paint.setShader(shader, Paint.ShaderType.LINEAR_SHADER);
        MaskFilter maskFilter = new MaskFilter(shadowWidth - 5, MaskFilter.Blur.OUTER);
        paint.setMaskFilter(maskFilter);
        return paint;

    }


    /**
     * 打印信息
     * @param output
     */
    private static void log(String output) {
        HiLog.info(sLogLabel, output);
    }

}
